package Buscaminas;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Main extends JFrame implements ActionListener {
	private int YScreen;
	private int XScreen;
	private int width;
	private int height;
	private int x;
	private int y;
	private JButton[][] botones;
	private boolean[][] arrayMinas;
	private Random r;
	private JPanel panelBotones;
	private int i;
	private int j;
	private int coor1;
	private int coor2;
	private int minasAlrededor;

	public Main() {
		this.r = new Random();
		this.inicializarVentana();
		this.setventana();
		colocarBotones();
		colocarMinas();
		visualizarMinas();
		setArrayMinas(this.arrayMinas);
		visualizarMinasAlrededor(i, j);
		setVisible(true);

	}

	public void setArrayMinas(boolean[][] arrayMinas) {
		this.i = r.nextInt(9);
		this.j = r.nextInt(9);
		if (arrayMinas[i][j] == false) {
			arrayMinas[i][j] = true;
		}
	}

	public void visualizarMinas() {
		for (int i = 0; i <= 8; i++) {
			for (int j = 0; j <= 8; j++) {
				if (arrayMinas[i][j]) {
					System.out.print("*");
				} else {
					System.out.print("-");
				}
			}
			System.out.println("");
		}
	}

	public int visualizarMinasAlrededor(int x, int y) {
		minasAlrededor = 0;
		if (arrayMinas[x][y]) {
			minasAlrededor = 9;
		}
		for (int i = x - 1; i <= x + 1; i++) {
			for (int j = y - 1; j <= y + 1; j++) {
				if (i >= 0 && i <= 8 && j <= 8 && j >= 0 && (i != x || j != y)) {
					if (arrayMinas[i][j]) {
						minasAlrededor++;
						System.out.println(minasAlrededor);
					}
				} else {
					System.out.println("-");
				}
			}
		}
		return minasAlrededor;
	}

	private void setventana() {

		JPanel panel = new JPanel(new BorderLayout());
		panelBotones = new JPanel(new GridLayout(9, 9));
		this.add(panel);
		JButton b = new JButton("Borrar");
		b.setActionCommand("borrar");
		b.addActionListener(this);
		panel.add(b, BorderLayout.NORTH);
		panel.add(panelBotones, BorderLayout.CENTER);
		arrayMinas = new boolean[9][9];
		botones = new JButton[9][9];
	}

	private void colocarBotones() {
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				botones[i][j] = new JButton();
				panelBotones.add(botones[i][j]);
				botones[i][j].addActionListener(this);
				botones[i][j].setActionCommand(i + "," + j);

			}
		}

	}

	private void colocarMinas() {
		int i = r.nextInt(9);
		int j = r.nextInt(9);
		int mina = 0;
		while (mina < 10) {
			if (arrayMinas[i][j] == false) {
				arrayMinas[i][j] = true;
				System.out.println(i + "," + j);
			}
			;
			mina++;
			i = r.nextInt(9);
			j = r.nextInt(9);
		}

	}

	private void inicializarVentana() {
		this.width = 300;
		this.height = 300;
		this.XScreen = Toolkit.getDefaultToolkit().getScreenSize().width;
		this.YScreen = Toolkit.getDefaultToolkit().getScreenSize().height;
		this.x = XScreen / 2 - width / 2;
		this.y = YScreen / 2 - height / 2;
		setLocation(x, y);
		setTitle("Window");
		setSize(width, height);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new Main();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		switch (e.getActionCommand()) {
		case "borrar":
			System.out.println("BORRAR");
			;
			break;
		default:
			conocerCoordenadasBoton(e);
			break;
		}
	}

	private void conocerCoordenadasBoton(ActionEvent e) {
		if (minasAlrededor != 0) {
			((JButton) e.getSource()).setText(String.valueOf(minasAlrededor));
		}
		System.out.println(e.getActionCommand());
		((JButton) e.getSource()).setEnabled(false);
		this.coor1 = Integer.parseInt(e.getActionCommand().split(",")[0]);
		this.coor2 = Integer.parseInt(e.getActionCommand().split(",")[1]);
		if (arrayMinas[coor1][coor2]) {
			System.out.println("Kaboom");
			((JButton) e.getSource()).setText(String.valueOf(minasAlrededor));
		}
		visualizarMinasAlrededor(coor1, coor2);

		limpiar(coor1, coor2);
	}

	private void limpiar(int x, int y) {
		botones[x][y].setEnabled(false);
		if (visualizarMinasAlrededor(x, y) != 0) {
			botones[x][y].setText(Integer.toString(visualizarMinasAlrededor(x,
					y)));
		}
		for (int incX = -1; incX <= 1; incX++) {
			for (int incY = -1; incY <= 1; incY++) {
				if (x + incX >= 0 && x + incX < 9 && y + incY >= 0
						&& y + incY < 9
						&& botones[x + incX][y + incY].isEnabled()) {
					if (visualizarMinasAlrededor(x + incX, y + incY) == 0) {
						limpiar(x + incX, y + incY);
					} else {
						if (visualizarMinasAlrededor(x + incX, y + incY) != 9) {
							botones[x + incX][y + incY].setEnabled(false);
							botones[x + incX][y + incY].setText(Integer
									.toString(visualizarMinasAlrededor(
											x + incX, y + incY)));
						}
					}
				}
			}
		}
	}
}
